package com.tpc.yaoxpt.service;


import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.tpc.yaoxpt.main.LogModule;
import com.tpc.yaoxpt.main.MainController;
import com.tpc.yaoxpt.utils.*;
import javafx.application.Platform;

import java.io.IOException;
import java.sql.Connection;
import java.util.Date;

public class BusinessService {
    final LogModule logModule = LogModule.getInstance();

    public void getBusinessInfo(){

        String intType = "C005";
        String intName = "获取医疗机构";
        JSONObject jsonObject = new JSONObject();
        Integer pageNumber = Integer.valueOf(FileUtil.getValue(intType, "PAGENUMBER"));
        String url = FileUtil.getValue("PTCONFIG","URL");
        String month = FileUtil.getValue(intType,"MONTH");

        url = url + "v2/delv/drug/medins/page";
        jsonObject.put("month",month);
        jsonObject.put("currentPageNumber", pageNumber );
//        logModule.appendLogInfo( "\n" + StringUtil.getInString(intType,intName,jsonObject.toString()));
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                MainController.staticLogText.appendText("\n"+StringUtil.getInString(intType,intName,jsonObject.toString()));
            }
        });
        String returnInfo = HttpClientUtil.doPost(url, jsonObject);
//        logModule.appendLogInfo( "\n" + StringUtil.getOutString(intType,intName,returnInfo));
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                MainController.staticLogText.appendText("\n"+StringUtil.getOutString(intType,intName,returnInfo));
            }
        });
        JSONObject outJSON = JSONObject.parseObject(returnInfo);
        if(outJSON.getInteger("returnCode")==1){
            if(outJSON.getInteger("currentPageNumber")<outJSON.getInteger("totalPageCount")){
                Connection connection = null ;
                try {
                    connection = JDBCUtil.getConnection();
                    TableSaveUtil.tableSave(connection,FileUtil.getValue(intType,"TABLE"),outJSON.getJSONArray("dataList"));
                    FileUtil.addSection(intType,"PAGENUMBER", String.valueOf(pageNumber+1));
                } catch (IOException e) {
//                    logModule.appendLogInfo( "\n" + StringUtil.getErrorString(intType,intName,e.getMessage()));
                    Platform.runLater(new Runnable() {
                        @Override
                        public void run() {
                            MainController.staticLogText.appendText("\n"+StringUtil.getErrorString(intType,intName,e.getMessage()));
                        }
                    });

                    throw new RuntimeException(e);
                }finally {
                    JDBCUtil.close(connection);
                }
            }else{
                try {
                    FileUtil.addSection(intType,"PAGENUMBER", String.valueOf(1));
                    FileUtil.addSection(intType,"MONTH", DateUtil.format(new Date(), DatePattern.NORM_MONTH_PATTERN));
                } catch (IOException e) {
//                    logModule.appendLogInfo( "\n" + StringUtil.getErrorString(intType,intName,e.getMessage()));
                    Platform.runLater(new Runnable() {
                        @Override
                        public void run() {
                            MainController.staticLogText.appendText("\n"+StringUtil.getErrorString(intType,intName,e.getMessage()));
                        }
                    });
                    throw new RuntimeException(e);
                }
            }
        }

    }
}
